var $sWidth = $(window).width();
var $sHeight = $(window).height();
$(function(){
	$(".shadowDiv").click(function(){
		var id = $(this).attr("id");
		var idx = id.lastIndexOf("_shadowDiv");
		if(idx!=-1){
			var did = id.substring(0,idx);
			hideDialog(did);
		}
	});
});
//初始化弹出框位置
function initDialogPosition(id){
	var dWidth = $("#"+id).width();
	var dHeight = $("#"+id).height();
	$("#"+id).css("top",-dHeight-100);
	$("#"+id).css("left",($sWidth-dWidth)/2);
}
//显示弹出框
function showDialog(id,time,isReload){
	$("#"+id).css("display","block");
	var dWidth = $("#"+id).width();
	var dHeight = $("#"+id).height();
	$("#"+id+"_shadowDiv").css("display","block");
	$("#"+id).css("left",($sWidth-dWidth)/2);
	$("#"+id).animate(
		{"top":($sHeight-dHeight)/2-100},
		"slow",
		function() { 
			if(time!=undefined){
				setTimeout("hideDialog('"+id+"',"+isReload+")",time);
			}
		}
	);
	plugin.drag($("#"+id+" .dialogTipDiv"),$("#"+id));
}
//隐藏弹出框
function hideDialog(id,isReload,func){
	var dWidth = $("#"+id).width();
	var dHeight = $("#"+id).height();
	$("#"+id+" .dialogContentDiv input").val("");
	$("#"+id+"_shadowDiv").css("display","none");
	$("#"+id).css("left",($sWidth-dWidth)/2);
	$("#"+id).animate(
		{
			"top":-dHeight-100},
			"slow",
			function(){
				$("#"+id).css("top",-dHeight-100);
				if(isReload!=undefined&&isReload){
					window.location.reload();
				}
				if(func){
					func();
				}
			}
		);
}
//转时间戳
function getTimeStamp(time){
	if(time.length == 19){
		var year = time.substring(0,4);
		var month = time.substring(5,7);
		var day = time.substring(8,10);
		var hour = time.substring(11,13);
		var minute = time.substring(14,16);
		var second = time.substring(17,19);
		var date = new Date(year*1,(month*1)-1,day*1,hour*1,minute*1,second*1).getTime();
		return date;
	}else if(time.length == 16){
		var year = time.substring(0,4);
		var month = time.substring(5,7);
		var day = time.substring(8,10);
		var hour = time.substring(11,13);
		var minute = time.substring(14,16);
		var second = 0;
		var date = new Date(year*1,(month*1)-1,day*1,hour*1,minute*1,second*1).getTime();
		return date;
	}else if(time.length == 10){
		var year = time.substring(0,4);
		var month = time.substring(5,7);
		var day = time.substring(8,10);
		var hour = 1;
		var minute = 0;
		var second = 0;
		var date = new Date(year*1,(month*1)-1,day*1,hour*1,minute*1,second*1).getTime();
		return date;
	}
	return new Date().getTime();
}
function getTimeStr(time){
	var date = new Date(time);
	var year=date.getFullYear();
    var month=date.getMonth()+1;
    var day=date.getDate();
    var hour=date.getHours();
    var minute=date.getMinutes();
    var second=date.getSeconds();
    
    return year+"-"+checkTime(month)+"-"+checkTime(day)+" "
    +checkTime(hour)+":"+checkTime(minute)+":"+checkTime(second);     
}
function checkTime(time){
	if(time<10){
		time = "0"+time;
	}
	return time;
}

function reload(){
	window.location.reload();
}

function dsSecurityRankCheck(json,message){
	var code = json.code;
	if(code=="200" || code==200){
		 plugin.msgtip({type: "o", msg: "操作成功"});
		 closeCodeWindow();
	 }else if(code=="needKey"){
		 plugin.msgtip({type: "e", msg: "请插入正确的key"});
	 }else if(code=="needCode"){
		 /*plugin.msgtip({type: "e", msg: "请输入验证码"});*/
		 showCodeWindow();
		 $("#codeMobile").html(json.mobile.substring(0,3)+"****"+json.mobile.substring(7,11));
		 return;
	 }else if(code=="codeError"){
		 plugin.msgtip({type: "e", msg: "动态验证码错误，请重新输入"});
		 $("#dynamicCode").val("");
		 showCodeWindow();
		 $("#codeMobile").html(json.mobile.substring(0,3)+"****"+json.mobile.substring(7,11));
		 return;
	 }else if(code=="needKeyCode"){
		 plugin.msgtip({type: "q", msg: "请插入key并输入动态验证码"});
		 $("#dynamicCode").val("");
		 showCodeWindow();
		 $("#codeMobile").html(json.mobile.substring(0,3)+"****"+json.mobile.substring(7,11));
		 return;
	 }else{
		 
		 if(message){
			 plugin.msgtip({type: "e", msg: "操作失败，原因:"+message});
		 }else if(json.message){
			 plugin.msgtip({type: "e", msg: "操作失败，原因:"+json.message});
		 }else{
			 plugin.msgtip({type: "e", msg: "操作失败，失败代码:"+code});
		 }
	 }
}
var reSendTime = 60;
function sendCodeToMobile(){
	if(reSendTime > 0){
		$("#sendCodeBtn").attr("disabled","disabled");
		$("#sendCodeBtn").css({"background":"#ccc","cursor":"default"});
		$("#sendCodeBtn").val(reSendTime+"s");
		setTimeout("sendCodeToMobile()", 1000);
		reSendTime --;
	}else{
		$("#sendCodeBtn").removeAttr("disabled");
		$("#sendCodeBtn").css({"background":"#00C27B","cursor":"pointer"});
		$("#sendCodeBtn").val("获取验证码");
		reSendTime = 60;
	}
}
function send(){
	var service = new EbeimService();
	$("#sendCodeBtn").attr("disabled","disabled");
	/*plugin.msgtip({type: "q", msg: "等待系统向您的预留手机发送验证码..."});*/
	service.sendMobileCode("{}",sendMobileCodeCB);
}
var sendMobileCodeCB = function(data){
	var json = data;
	if(json.code == "200"){
		plugin.msgtip({type: "o", msg: "系统已向您的预留手机发送了验证码。请注意查收！"});
	}else{
		plugin.msgtip({type: "e", msg: "抱歉，系统无法向您的手机发送验证码！请查看手机号码是否正确。"});
	}
	sendCodeToMobile();
}

function toPage(url){
	window.location.href = url;
}

function getParameter(name) {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null) return unescape(r[2]); return null;
}

function closeCodeWindow(){
	if($popform){
		$popform.hide();
	}
}

var $popform;
function showCodeWindow(){
	var content = "<table class='code-table' border='0' align='center'>"
		+"<tr>"
		+"<td>"
		+"<span>手机号:</span><span id='codeMobile' class='inp-phone'></span>"
		+"</td>"
		+"</tr>"
		+"<tr>"
		+"<td>"
		+"<span>验证码:</span><input class='inp-code' type='text' id='dynamicCode' /><input type='button' id='sendCodeBtn' onClick='send()' class='btn btn-code' value='获取验证码'>"
		+"</td>"
		+"</tr>"
		+"</table>";
	closeCodeWindow();
	var popform = new plugin.popform({
	    title: "请输入动态验证码",
	    className: "code-window",
	    content: content,
	    width: 350,
	    destroy: true,
	    onHide: function () {
	        // 隐藏弹窗时要执行的回调函数
	    },
	    onShow: function () {
	        // 显示弹窗时要执行的回调函数
	    	$("#dynamicCode").keydown(function(){
	    		if(event.keyCode == 13) { 
	    			setDynamicCode();
	    		}  
	    	})
	    },
	    btns: [{
	        value: "确定",
	        id:"submit",
	        className: "btn btn-success",
	        fn: function () {
	        	setDynamicCode();
	        }
	    },{
	        value: "取消",
	        className: "btn btn-cancel",
	        id:"sendCodeBtn",
	        fn: function () {
	        	popform.hide();
	        }
	    }]
	}).show();
	$popform = popform;
}

function fileSizeTransfer(size){
	var fileSize = size;
	if(size/1024 > 1){
		fileSize = size/1024.0;
		fileSize = fileSize.toFixed(2);
		fileSize += " K";
	}else{
		fileSize += " B";
	}
	if(size/(1024*1024) > 1){
		fileSize =size/(1024.0*1024);
		fileSize = fileSize.toFixed(2);
		fileSize += " M";
	}
	if(size/(1024*1024*1024) > 1){
		fileSize = size/(1024.0*1024*1024);
		fileSize = fileSize.toFixed(2);
		fileSize += " G";
	}
	return fileSize;
}

function fileSizeTransferNoPoint(size){
	var fileSize = size;
	if(size/1024 > 1){
		fileSize = size/1024.0;
		fileSize = fileSize.toFixed(0);
		fileSize += " K";
	}else{
		fileSize += " B";
	}
	if(size/(1024*1024) > 1){
		fileSize =size/(1024.0*1024);
		fileSize = fileSize.toFixed(0);
		fileSize += " M";
	}
	if(size/(1024*1024*1024) > 1){
		fileSize = size/(1024.0*1024*1024);
		fileSize = fileSize.toFixed(0);
		fileSize += " G";
	}
	return fileSize;
}

